import dfish from 'dfish-widget';
import { debug, baseURL } from '@/config';
import { FromApp, NoLoginCode, OtherLoginCode } from '@/common/constants';
import 'dfish-widget/css/style.css';
import 'dfish-widget/css/reset.css';
import { showWarn } from '@/common/utils';
// import router from '@/router';
// import useUserStore from '@/stores/user';

dfish.config({
  debug,
  lang: 'zh-CN',
  autoPlaceholder: true,
  cnBytes: 3,
  downloadTarget: '_self',
  http: {
    baseURL,
    headers: {
      post: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      'x-requested-with': 'dfish',
      fromApp: FromApp
    }
    //withCredentials: true
  },
  defaultOptions: {
    'w-dialog': {
      height: 350,
      width: 680
    },
    'w-table': {
      scrollY: true,
      scrollX: true
    },
    'w-pagebar': {
      align: 'right',
      valign: 'middle',
      height: 40
    },
    'w-tree': {
      scrollY: true,
      height: '*'
    },
    'w-tree-v2': {
      height: '*'
    },
    'w-grid': {
      scrollY: true,
      gap: 10
    },
    'w-label': {
      width: 150
    },
    'w-toggle': {
      collapsedIcon: '.f-i .f-i-plus',
      expandedIcon: '.f-i .f-i-minus'
    },
    'w-textarea': {
      inputHeight: 100,
      showCount: true
    },
    'w-spinner': {
      autoFix: false,
      inputWidth: 200
    },
    'w-switch': {
      checkedValue: '1',
      checkedText: '是',
      uncheckedValue: '0',
      uncheckedText: '否'
    },
    'w-imageupload': {
      uploadSrc: 'support/file/upload/image',
      thumbnailSrc: 'support/file/inline/$id',
      fileTypes: '*.jpg;*.png;*.jpeg;',
      uploadLimit: 9,
      filter: (res) => res.data.content
    },
    'w-fileupload': {
      uploadSrc: 'support/file/upload/file',
      downloadSrc: 'support/file/download/$id',
      filter: (res) => res.data.content
    },
    'w-mediaupload': {
      uploadSrc: 'file/upload/file',
      thumbnailSrc: 'file/inline/$id',
      uploadLimit: 9,
      filter: (res) => res.data.content
    },
    'w-datepicker': {
      inputWidth: 230
    },
    'w-combobox': {
      clearable: false
    },
    'w-dropbox': {
      clearable: false
    }
  }
});

dfish.http.interceptors.request.use((config) => {
  const { token } = useUserStore();
  if (token) {
    config.headers.token = token;
  }
  if (config.url.startsWith('/api')) {
    if (process.env.NODE_ENV === 'development') {
      config.baseURL = '/';
    } else {
      config.baseURL = 'http://192.168.118.70:8085';
    }
  }
  return config;
});

dfish.http.interceptors.response.use(
  (response) => {
    const { error } = response.data;
    if ([NoLoginCode, OtherLoginCode].includes(error?.code)) {
      if (OtherLoginCode === error.code) {
        showWarn(error.message);
      }
      const { clearUserStore } = useUserStore();
      clearUserStore();
      router.replace('/login');
      return;
    }
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);

window.$ = dfish;
export default dfish;
